package com.wwf.controller;

import com.wwf.fegin.IEmailService;
import com.wwf.service.IAuthCodeService;
import com.wwf.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lulei
 * @date 2021年02月04日 15:49:36
 */
@RestController
@RequestMapping("/code")
public class AuthCodeController {
    @Autowired
    IAuthCodeService authCodeService;
    @Autowired
    IEmailService emailService;
    /**
     *
     * 生成验证码
     * @param email 邮箱
     * @return
     */
    @GetMapping("/create/{email}")
    public Result<Map> create(@PathVariable("email") String email){
        String qcode = authCodeService.createCode(email);
        Map<String,String> maprs = new HashMap<String,String>();
        //maprs.put("qcode",qcode);
        //发送邮箱
        emailService.email(email,qcode);
        return new Result("200",maprs);
    }

    /**
     *
     * 验证验证码是否有效
     * @param email 邮箱
     * @param code 验证码
     * @return
     */
    @GetMapping("/validate/{email}/{code}")
    public String validate(@PathVariable("email") String email,@PathVariable("code") String code){
        //Map<String,Object> maprs = new HashMap<String,Object>();
        boolean rs = authCodeService.checkCode(email, code);
        if(rs){
            return "1";
        }else{
            return "0";
        }
    }
}
